package cn.edu.scau.dormitory_management_system.module.assign_dormitory.service.Impl;

import cn.edu.scau.dormitory_management_system.aop.annotation.ManyDown;
import cn.edu.scau.dormitory_management_system.entity.BedEntity;
import cn.edu.scau.dormitory_management_system.entity.DormEntity;
import cn.edu.scau.dormitory_management_system.entity.StudentEntity;
import cn.edu.scau.dormitory_management_system.entity.SysLogEntity;
import cn.edu.scau.dormitory_management_system.entity.dto.LogDTO;
import cn.edu.scau.dormitory_management_system.module.assign_dormitory.service.AssignDormitory;
import cn.edu.scau.dormitory_management_system.service.BedService;
import cn.edu.scau.dormitory_management_system.service.DormService;
import cn.edu.scau.dormitory_management_system.service.StudentService;
import cn.edu.scau.dormitory_management_system.service.SysLogService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @ClassName AssignDormitoryImpl
 * @Description TODO
 * @Author wlf
 * @Date 2021/8/17 10:23
 * @Version 1.0
 **/
@Service
public class AssignDormitoryImpl implements AssignDormitory {
    @Autowired
    BedService bedService;
    @Autowired
    DormService dormService;
    @Autowired
    StudentService studentService;

    /**
     * 分配单个学生宿舍
     *
     * @param studentId
     * @param dormId
     * @return
     */
    @ManyDown
    @Override
    public synchronized boolean assignDormitory_Singleton(String studentId, int dormId) {
        final BedEntity bedEntity = bedService.getByDormId(dormId);
        if (bedEntity == null) {
            return false;
        }
        bedEntity.setStuId(studentId);
        bedEntity.setStatus(0);
        bedEntity.setLiveTime(new Date());

        Date date = new Date();
        bedService.update(bedEntity);
        sum1 += new Date().getTime() - date.getTime();

        final DormEntity dormEntity = dormService.getById(dormId);
        dormEntity.setStuNum(dormEntity.getStuNum() + 1);
        if (dormEntity.getStatus() == 1) {
            StudentEntity studentEntity = studentService.getOne(new QueryWrapper<StudentEntity>().eq("stu_id", studentId));
            dormEntity.setClazz(studentEntity.getClazz());
            dormEntity.setAcademic(studentEntity.getAcademic());
            dormEntity.setStatus(0);
        }

        date = new Date();
        dormService.update(dormEntity);
        sum2 += new Date().getTime() - date.getTime();

        return true;
    }

    @Override
    public synchronized boolean assignDormitory_Batch(StudentEntity studentEntity) {
        if (studentEntity == null) {
            return false;
        }
        DormEntity dormEntity = dormService.select(studentEntity.getAcademic(), studentEntity.getClazz(), studentEntity.getDepartment());
        if (dormEntity == null) {
            return false;
        }
        return assignDormitory_Singleton(studentEntity.getStuId(), dormEntity.getDormId());
    }

    /**
     * 调宿--自己选择
     *
     * @param studentId
     * @param dormId
     * @return
     */
    @Transactional
    @Override
    public synchronized boolean updateDormitory(String studentId, int dormId) {
        final boolean flag = retreatDormitory(studentId, dormId);
        if (!flag) {
            return false;
        }
        return assignDormitory_Singleton(studentId, dormId);
    }

    /**
     * 退宿
     *
     * @param studentId
     * @param dormId
     * @return
     */
    @Override
    public boolean retreatDormitory(String studentId, int dormId) {
        final BedEntity bedEntity = bedService.getByStuId(studentId);
        bedEntity.setStatus(1);
        bedEntity.setStuId("");
        bedEntity.setLiveTime(null);
        final boolean update = bedService.update(bedEntity);
        if (!update) {
            return false;
        }
        final DormEntity dormEntity = dormService.getById(dormId);
        dormEntity.setStuNum(dormEntity.getStuNum() - 1);
        if (dormEntity.getStuNum() == 0) {
            dormEntity.setStatus(1);
        }
        final boolean update1 = dormService.update(dormEntity);
        if (!update1) {
            return false;
        }
        return true;
    }

    @Autowired
    private LogDTO logDTO;
    @Autowired
    private SysLogService logService;
    private long sum1, sum2;

    private void addLog(long ms, int sum, String table) {
        SysLogEntity logEntity = new SysLogEntity();
        logEntity.setIpAddr(logDTO.getIp());
        logEntity.setCity(logDTO.getCity());
        logEntity.setUserName(logDTO.getUserName());
        logEntity.setExeTime(ms);
        String ops = "";
        if(sum > 1) ops = "批量";
        logEntity.setOperation("在表" + table + "中" + ops + "修改" + sum + "条记录");
        logEntity.setOpTime(new Date());
        logService.save(logEntity);
    }

    @Override
    public void insertList(List<StudentEntity> list) {
        logDTO.setCan(false);
        sum1 = sum2 = 0;
        int ans = 0;
        for (StudentEntity studentEntity : list) {
            ans += assignDormitory_Batch(studentEntity) ? 1 : 0;
        }
        if(ans>0) {
            addLog(sum1, ans, "tb_bed");
            addLog(sum2, ans, "tb_dorm");
        }
        logDTO.setCan(true);
    }

}
